package fr.imag.adele.mediation.probes.impl;

import java.util.Dictionary;
import java.util.StringTokenizer;

import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;

import fr.imag.adele.mediation.probes.services.DataSender;
import fr.imag.adele.mediation.probes.services.Probe;
import fr.imag.adele.mediation.probes.services.Scheduler;
import fr.imag.adele.mediation.probes.util.DataEvent;
import fr.imag.adele.mediation.probes.util.DataSet;

public class StandardDataSender implements DataSender {
	private String	 		m_providedResources;	
	private String[]		m_resources;
	private DataSet 		m_localDataSet = new DataSet();	
	
	private ServiceTracker 	m_tracker;		
	private BundleContext 	m_bundleContext;			
	private EventAdmin 		ea;
	
	public StandardDataSender(BundleContext bc){
		this.m_bundleContext = bc;		
	}
	public String removeSpaces(String s) {
		  StringTokenizer st = new StringTokenizer(s," ",false);
		  String t="";
		  while (st.hasMoreElements()) t += st.nextElement();
		  return t;
		  }
	public void start(){
		
		m_tracker = new ServiceTracker(m_bundleContext, EventAdmin.class.getName(), null);
		m_tracker.open();
		ea = (EventAdmin)getTracker().getService();
		m_providedResources = removeSpaces(m_providedResources);
		m_resources = m_providedResources.split(",");
	}
	public void stop(){
		m_tracker.close();
	}
	protected ServiceTracker getTracker(){
		return m_tracker;
	}
	/*
	 * Test whether this event is provided by the actual probe
	 * @return true if it is provided
	 */
	protected boolean isProvidedResource(String resource){
		if (m_resources != null){
			for (int i=0; i<m_resources.length; i++){
				if (m_resources[i].equals(resource))
					return true;		
			}
		}
		return false;
	}	
	/*
	 * sendData
	 * Called by the probe to send the calculated data.
	 * We store the data locally until the scheduler pick up.
	 * @see fr.imag.adele.mediation.probes.services.DataSender#sendData(fr.imag.adele.mediation.probes.util.DataSet)
	 */
	public void sendData(DataSet dataSet) {
		System.out.println("[PROBE] Sending...");
		if (dataSet != null) {
			for (int i=0; i<dataSet.size(); i++){			
				send(dataSet.getDataEvent(i));				
			}
		}
	}
	/*
	 * send
	 * Internal function that send collected data on the dataSender to destinations.
	 * It is called by the Schedular.
	 */
	protected void send(DataEvent dataEvent){
		// verify that the probe is specified to send that kind of events.
		if (dataEvent != null && isProvidedResource(dataEvent.getResource())==true){			
			if (ea != null){
				ea.sendEvent(new Event(dataEvent.getSource() + "/" + dataEvent.getResource(), dataEvent.getData()));
				System.out.println("[" + dataEvent.getSource() 
						+ ":"+ dataEvent.getTimeStamp() +"] " + dataEvent.getResource()
						+ " "  + dataEvent.getValue()
						+ " "  + dataEvent.getUnit());
			}
		}
	}
}
